﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using BBInterfaceNET.Services;
using Microsoft.Practices.Prism.ViewModel;
using System.ComponentModel;
using BBInterfaceNET.Model;

namespace BBInterfaceNET.ViewModels
{
    public class NewProjectViewModel:INotifyPropertyChanged
    {
        private List<OsInfo> osInfos;
        private OsInfo selOs;
        private DeviceInfo selDev;

        private string projectPath;
        private IInteractionService interactionService;
        public NewProjectViewModel(IInteractionService interactionService)
        {
            this.interactionService=interactionService;
        }
        public string Title { get; set; }
        public string ProjectName { get; set; }
        public string ProjectPath 
        {
            get { return projectPath; }
            set
            {
                if (projectPath != value)
                {
                    projectPath = value;
                    OnPropertyChanged("ProjectPath");
                }
            }
        }
        public bool CreateProjectDirectory { get; set; }
        public bool? Result { get; set; }

        private DelegateCommand browseCmd;
        public ICommand BrowseCommand
        {
            get
            {
                if (browseCmd == null)
                    browseCmd = new DelegateCommand(OnBrowse);
                return browseCmd;
            }
        }

        private void OnBrowse()
        {
            string path = interactionService.GetDirectory();
            if (!string.IsNullOrEmpty(path))
            {
                ProjectPath = path;
            }
        }

        public List<OsInfo> OsInfos
        {
            get
            {
                if (osInfos == null)
                    osInfos = OsInfo.GetOsInfos();
                SelectedOs = osInfos[0];
                return osInfos;
            }
        }
        public OsInfo SelectedOs
        {
            get { return selOs; }
            set
            {
                if (selOs != value)
                {
                    selOs = value;
                    selDev = selOs.Devices[0];
                    OnPropertyChanged("SelectedOs");
                    OnPropertyChanged("SelectedDevice");
                }
            }
        }
        public DeviceInfo SelectedDevice
        {
            get { return selDev; }
            set
            {
                if (selDev != value)
                {
                    selDev = value;
                    OnPropertyChanged("SelectedDevice");
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}
